#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Repo Compare Stable ===
function repocomparestable()
{
  # Usage: repocomparestable [stable_ZNH5Y] [github_account_LineageOS] [full_diff] (Helper to compare with stable branch)

  # Default variables
  local default_github_account=LineageOS;
  local default_to_insert=ZNH5Y;

  # Variables
  local stable_tag=${1:-${default_to_insert}};
  local github_account=${2:-${default_github_account}};
  local full_diff=${3:-};

  # Diff execution
  echo '';
  echo -n ' Scanning...';
  repo forall -c "

     # Filter out projects
     if [ -z \$(echo \"\${REPO_PROJECT}\" | grep -i \"${github_account}\") ]; then
       echo -en  \"\r\033[K Ignoring \${REPO_PROJECT} [\${REPO_REMOTE}/\${REPO_RREV##*/}] \";
       return;
     fi;

     # Parse the branch
     project_branch=\$(echo \"\${REPO_RREV##*/}\" \
                     | sed 's/stable\///' \
                     | sed 's/-${stable_tag}//');

     # Show and fetch projects
     echo -en  \"\r\033[K Scanning \${REPO_PROJECT} [\${REPO_REMOTE}/\${REPO_RREV##*/}] \";
     git fetch --quiet \${REPO_REMOTE} \${project_branch};

     # Detect local differences
     if [ \$(git diff --quiet HEAD FETCH_HEAD; echo \${?}) -ne 0 ]; then
       echo -e  \"\r\033[K==========================================================\";
       echo -e  \"Project \${REPO_PROJECT} [\${REPO_RREV##*/} / \${project_branch}]\";
       if [ -z \"${full_diff}\" ]; then
         git --no-pager diff --stat HEAD FETCH_HEAD  -- . \":(exclude)*res/*-*/*\";
       elif [ ! -z \"\${default_see_translations}\" ]; then
         git --no-pager diff HEAD FETCH_HEAD  -- . \":(exclude)*res/*-*/*\";
       else
         git --no-pager diff HEAD FETCH_HEAD;
       fi;
       echo -n ' Stable commits / Mainline new commits :  ';
       git rev-list --left-right --count HEAD...FETCH_HEAD;
       echo '';
     fi;

     # Detect local differences
     if [ \$(git diff --quiet; echo \${?}) -ne 0 ]; then
       echo -e  \"\r\033[K==========================================================\";
       echo \"Project \${REPO_PROJECT} / \${project_branch}: Unmerged differences\";
       git --no-pager diff;
       echo '';
     fi;
    " 2> /dev/null;

  # Done
  echo -en  '\r\033[K Done searching';
  echo '';
}

# === Repo Compare Upstream ===
function repocompareupstream()
{
  # Usage: repocompareupstream [github_account_LineageOS] [full_diff] (Helper to compare with upstream branch)';

  # Default variables
  local default_github_account=LineageOS;
  local default_filtered_projects=chromium-webview;

  # Variables
  local upstream_branch;

  # Upstream branch
  upstream_branch=$(grep revision .repo/manifest.xml 2> /dev/null \
                  | grep android- \
                  | head -n 1 \
                  | sed 's/.*"\(refs.*\)".*/\1/');

  # Variables
  local github_account=${1:-${default_github_account}};
  local full_diff=${2:-};

  # Diff execution
  echo '';
  echo -n ' Scanning...';
  repo forall -c "

     # Filter out projects
     if [ -z \$(echo \"\${REPO_PROJECT}\" | grep -i \"${github_account}\") ] || \
             [ ! -z \$(echo \"\${REPO_PROJECT}\" | grep -i \"${default_filtered_projects}\") ]; then
       echo -en  \"\r\033[K Ignoring \${REPO_PROJECT} [\${REPO_REMOTE}/\${REPO_RREV##*/}] \";
       return;
     fi;

     # Create the AOSP path
     project_upstream=https://android.googlesource.com;
     if (echo \${REPO_PATH} | grep -qv '^device'); then
         project_upstream=\${project_upstream}/platform;
     fi
     project_upstream=\${project_upstream}/\${REPO_PATH};

     # Show and fetch projects
     echo -en  \"\r\033[K Scanning \${REPO_PROJECT} [\${REPO_REMOTE}/\${REPO_RREV##*/}] \";
     git fetch -v \${project_upstream} ${upstream_branch};

     # Detect new remote commits
     changescount=\$(git rev-list --right-only --count HEAD...FETCH_HEAD);
     if [ \${changescount} -ne 0 ]; then
       echo -en  \"\r\033[K Project \${REPO_PROJECT}: \${changescount} remote updates\";
       echo '';
       git log --pretty=oneline --abbrev-commit --max-count=\${changescount} FETCH_HEAD;
       echo '';
     fi;

    " 2> /dev/null;

  # Done
  echo -en  '\r\033[K Done searching';
  echo '';
}

# === Repo Compare Tags ===
function repocomparetags()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: repocomparetags <tag_to_compare_to_android_branch> [ignore_account_LineageOS] (Helper to compare with tags)';
    echo ' Information: Need to be started from the repo root';
    echo '';
    return;
  fi;

  # Default variables
  local default_ignore_account=LineageOS;
  local default_filtered_projects=chromium-webview;

  # Variables
  local upstream_branch;

  # Upstream branch
  upstream_branch=$(grep revision .repo/manifest.xml 2> /dev/null \
                  | grep android- \
                  | head -n 1 \
                  | sed 's/.*"\(refs.*\)".*/\1/');

  # Variables
  local upstream_to_compare=${1};
  local ignore_account=${2:-${default_ignore_account}};

  # Diff execution
  echo '';
  echo -n ' Scanning...';
  repo forall -c "

     # Filter out projects
     if [ ! -z \$(echo \"\${REPO_PROJECT}\" | grep -i \"${ignore_account}\") ] || \
             [ ! -z \$(echo \"\${REPO_PROJECT}\" | grep -i \"${default_filtered_projects}\") ]; then
       echo -en  \"\r\033[K Ignoring \${REPO_PROJECT} [\${REPO_REMOTE}/\${REPO_RREV##*/}] \";
       exit;
     fi;

     # Create the AOSP path
     project_upstream=https://android.googlesource.com;
     if (echo \${REPO_PATH} | grep -qv '^device'); then
         project_upstream=\${project_upstream}/platform;
     fi
     project_upstream=\${project_upstream}/\${REPO_PATH};

     # Show and fetch projects
     echo -en  \"\r\033[K Scanning \${REPO_PROJECT} [\${REPO_REMOTE}/\${REPO_RREV##*/}] \";
     if ! git fetch -v \${project_upstream} ${upstream_branch}; then
       exit;
     fi;
     upstream_head_current=\$(git rev-parse FETCH_HEAD);
     if ! git fetch -v \${project_upstream} ${upstream_to_compare}; then
       exit;
     fi;
     upstream_head_compare=\$(git rev-parse FETCH_HEAD);

     # Detect local differences
     if [ \$(git diff --quiet ${upstream_head_current} ${upstream_head_compare}; echo \${?}) -ne 0 ]; then
       echo -e  \"\r\033[K==========================================================\";
       echo \"Project \${REPO_PROJECT} / ${upstream_branch} & ${upstream_to_compare}\";
       git --no-pager diff ${upstream_head_current} ${upstream_head_compare};
       echo '';
     fi;

    " 2> /dev/null;

  # Done
  echo -en  '\r\033[K Done searching';
  echo '';
}
